﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace Tools.Helper
{
    public class JsonFormater
    {
        public event InvokeScriptCompletedEventHandler InvokeFormatCompleted;
        public void FormatAsync(string jsonStr)
        {
            StringBuilder scriptBuilder = new StringBuilder();
            JavascriptHelper helper = new JavascriptHelper();
            helper.AddScriptSrc(scriptBuilder, Path.Combine(Config.BaseDirectory, @"javascript\json.parser.js"));
            helper.AddScriptSrc(scriptBuilder, Path.Combine(Config.BaseDirectory, @"javascript\json.format.js"));
            string jsfunction = @"
                                function jsonformat(jsonStr)
                                {
                                    var returnVal;
                                    var error = false;
                                  //  try
                                    {
                                        jsl.parser.parse(jsonStr);
                                        returnVal = jsl.format.formatJson(jsonStr)
                                    }
                                   // catch (e)
                                  //  {
                                  //      returnVal = e.message;
                                  //      error = true;
                                 //   }
                                    return returnVal;
                                }
                                ";

            helper.AddScriptBlock(scriptBuilder, jsfunction);
            helper.InvokeScriptCompleted += new InvokeScriptCompletedEventHandler(helper_InvokeScriptCompleted);
            helper.InvokeScriptAsync(scriptBuilder, "jsonformat", new object[] { jsonStr });
        }

        void helper_InvokeScriptCompleted(InvokeScriptCompletedEventArgs e)
        {
            if (InvokeFormatCompleted != null)
            {
                InvokeFormatCompleted(new InvokeScriptCompletedEventArgs(e.Result));
            }
        }
    }

    public class JavascriptFormater
    {
        public event InvokeScriptCompletedEventHandler InvokeFormatCompleted;
        public void FormatAsync(string jsStr)
        {
            StringBuilder scriptBuilder = new StringBuilder();
            JavascriptHelper helper = new JavascriptHelper();
            helper.AddScriptSrc(scriptBuilder, Path.Combine(Config.BaseDirectory, @"javascript\jsformat.js"));
            //helper.AddScriptSrc(scriptBuilder, Path.Combine(Config.BaseDirectory, @"javascript\jsl.format.js"));
            string jsfunction = @"
                                function StringBuffer()
                                {
                                    this._strings_ = new Array;
                                }

                                StringBuffer.prototype.append = function (str)
                                {
                                    this._strings_.push(str);
                                };

                                StringBuffer.prototype.toString = function ()
                                {
                                    return this._strings_.join('');
                                };
                                function javascriptformat(jsStr)
                                {
                                    jsStr = jsStr.replace(/^\s+/, '');
                                    var lenStr = jsStr.length;
                                    var blockSise = 2000;
                                    var tmp;
                                    var tabsize = 4;
                                    tabchar = ' ';
                                    var result = new StringBuffer();

                                    for (var i = 0; i * blockSise < lenStr; i++)
                                    {
                                        var begin = i * blockSise;
                                        var end = (i + 1) * blockSise;
                                        tmp = jsStr.substring(begin, end);
                                        result.append(js_beautify(tmp, tabsize, tabchar));
                                    }

                                    return result.toString();
                                }
                                ";

            helper.AddScriptBlock(scriptBuilder, jsfunction);
            helper.InvokeScriptCompleted += new InvokeScriptCompletedEventHandler(helper_InvokeScriptCompleted);
            helper.InvokeScriptAsync(scriptBuilder, "javascriptformat", new object[] { jsStr });
        }

        void helper_InvokeScriptCompleted(InvokeScriptCompletedEventArgs e)
        {
            if (InvokeFormatCompleted != null)
            {
                InvokeFormatCompleted(new InvokeScriptCompletedEventArgs(e.Result));
            }
        }
    }
}
